using System;
using System.ComponentModel;

using DevExpress.Xpo;
using DevExpress.Xpo.Metadata;
using DevExpress.Data.Filtering;

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp.Security.Strategy;

namespace WaTP.Module.BusinessObjects
{
    [DefaultClassOptions]
    public class Quote : BaseObject
    {
        public Quote(Session session)
            : base(session)
        {
            // This constructor is used when an object is loaded from a persistent storage.
            // Do not place any code here or place it only when the IsLoading property is false:
            // if (!IsLoading){
            //    It is now OK to place your initialization code here.
            // }
            // or as an alternative, move your initialization code into the AfterConstruction method.
        }
        private string quoteID;
        public string QuoteID {
            get { return quoteID; }
            set { SetPropertyValue("QuoteID", ref quoteID, value); }
        }
        private DateTime effectFrom;
        public DateTime EffectFrom {
            get { return effectFrom; }
            set { SetPropertyValue("EffectFrom", ref effectFrom, value); }
        }
        private DateTime effectTo;
        public DateTime EffectTo {
            get { return effectTo; }
            set { SetPropertyValue("EffectTo", ref effectTo, value); }
        }
        private Department department;
        public Department Department {
            get { return department; }
            set { SetPropertyValue("Department", ref department, value); }
        }
        //
        private DateTime? completedDate;
        public DateTime? CompletedDate {
            get { return completedDate; }
            set { SetPropertyValue("CompletedDate", ref completedDate, value); }
        }
        //
        [Persistent("CreateOn"), ValueConverter(typeof(UtcDateTimeConverter))]
        protected DateTime _CreateOn = DateTime.Now;
        [PersistentAlias("_CreateOn")]
        [ModelDefault("EditMask", "G")]
        [ModelDefault("DisplayFormat", "{0:G}")]
        public DateTime CreateOn
        {
            get { return _CreateOn; }
        }
        internal virtual void UpdateCreatedOn()
        {
            UpdateCreatedOn(DateTime.Now);
        }
        internal virtual void UpdateCreatedOn(DateTime date)
        {
            _CreateOn = date;
            Save();
        }
        protected override void OnChanged(string propertyName, object oldValue, object newValue)
        {
            base.OnChanged(propertyName, oldValue, newValue);
            if (propertyName == "QuoteID")
            {
                UpdateCreatedOn();
            }
        }
        //User.
        [Persistent("User")]
        private SecuritySystemUser user;
        [PersistentAlias("user")]
        public SecuritySystemUser User
        {
            get { return user; }
            internal set { user = value; }
        }

        private QuoteStatus status;
        public QuoteStatus Status {
            get { return status; }
            set { SetPropertyValue("Status", ref status, value); }
        }
        public override void AfterConstruction()
        {
            base.AfterConstruction();
            user = Session.GetObjectByKey<SecuritySystemUser>(SecuritySystem.CurrentUserId);
            status = QuoteStatus.InProgress;
            // Place here your initialization code.
        }
        [Association("Quote-SaleItems"), Aggregated]
        public XPCollection<SaleItem> SaleItems {
            get { return GetCollection<SaleItem>("SaleItems");}
        }
    }

    public enum QuoteStatus {
        [ImageName("State_Task_InProgress")]
        InProgress,
        [ImageName("State_Task_WaitingForSomeoneElse")]
        WaitingForSomeoneElse,
        [ImageName("State_Task_Completed")]
        Completed,
        [ImageName("State_Validation_Invalid")]
        Canceled
    }

    public class SaleItem : BaseObject {
        public SaleItem(Session session) : base(session) { }
        private Product product;
        public Product Product {
            get { return product; }
            set { SetPropertyValue("Product", ref product, value); }
        }
        private int quantity;
        public int Quantity {
            get { return quantity; }
            set { SetPropertyValue("Quantity", ref quantity, value); }
        }
        private double price;
        public double Price {
            get { return price; }
            set { SetPropertyValue("Price", ref price, value); }
        }
        private bool waitingForSomeone;
        public bool WaitingForSomeone {
            get { return waitingForSomeone; }
            set { SetPropertyValue("WaitingForSomeone", ref waitingForSomeone, value); }
        }
        private SaleItemStatus status;
        public SaleItemStatus Status {
            get { return status; }
            set { SetPropertyValue("Status", ref status, value); }
        }
        private Quote quote;
        //User.
        [Persistent("User")]
        private SecuritySystemUser user;
        [PersistentAlias("user")]
        public SecuritySystemUser User
        {
            get { return user; }
            internal set { user = value; }
        }

        public override void AfterConstruction()
        {
            base.AfterConstruction();
            status = SaleItemStatus.InProgress;
            user = Session.GetObjectByKey<SecuritySystemUser>(SecuritySystem.CurrentUserId);
        }
        [Association("Quote-SaleItems")]
        public Quote Quote {
            get { return quote; }
            set { SetPropertyValue("Quote", ref quote, value); }
        }
    }
    public enum SaleItemStatus {
        [ImageName("State_Task_InProgress")]
        InProgress,
        [ImageName("State_Task_WaitingForSomeoneElse")]
        WaitingForSomeoneElse,
        [ImageName("State_Task_Completed")]
        Appove
    }
}
